package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

// Created on:	2-Jun-08
// Author:		Richard Shih

public class CallRecord
{

	private final String START_TIME = "start_time";
	private final String END_TIME = "end_time";
	private final String CALLER_ID = "caller_id";
	private final String T_EXT = "to_ext";
	private final String AMOUNT = "amount";
	
	private GregorianCalendar	startTime;
	private GregorianCalendar	endTime;
	private PhoneAccount		caller;
	private int					toExtension;
	private double				amount;

	public CallRecord(int id)
	{
		try
		{
			// DB setup
			Connection conn = getConnection();
			
			PreparedStatement ps;
			ResultSet rs;
	
			ps = conn.prepareStatement("SELECT * FROM callrecord WHERE id=?");
			ps.setInt(1, id);
			
			rs = ps.executeQuery();
			
			if(rs.next())
			{
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				
				int text = rs.getInt(T_EXT);
				double amt = rs.getDouble(AMOUNT);
				int cid = rs.getInt(CALLER_ID);
			
				//Instantiate the CallRecord
				this.startTime = new GregorianCalendar();
				startTime.setTime(df.parse(rs.getString(START_TIME)));
				//startTime.add(GregorianCalendar.MONTH, 1);
				
				this.endTime = new GregorianCalendar();
				endTime.setTime(df.parse(rs.getString(END_TIME)));
				//endTime.add(GregorianCalendar.MONTH, 1);
				
				this.toExtension = text;
				this.amount = amt;
				this.caller = new PhoneAccount(cid);
				
			}
			rs.close();
			conn.close();
		}
		catch(Exception e)
		{
			System.out.println("Error Instantiating the CallRecord");
		}
	}

	public String getFormattedStartTime()
	{
		SimpleDateFormat df = new SimpleDateFormat("EEE, MMM d, yyyy h:mm a");
		return df.format(startTime.getTime());
	}
	
	public String getSortStartTime()
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(startTime.getTime());
	}
	
	public String getFormattedEndTime()
	{
		SimpleDateFormat df = new SimpleDateFormat("EEE, MMM d, yyyy h:mm a");
		return df.format(endTime.getTime());
	}
	
	public GregorianCalendar getStartTime()
	{
		return startTime;
	}

	public void setStartTime(GregorianCalendar startTime)
	{
		this.startTime = startTime;
	}

	public GregorianCalendar getEndTime()
	{
		return endTime;
	}

	public void setEndTime(GregorianCalendar endTime)
	{
		this.endTime = endTime;
	}

	public PhoneAccount getCaller()
	{
		return caller;
	}

	public void setCaller(PhoneAccount caller)
	{
		this.caller = caller;
	}

	public int getToExtension()
	{
		return toExtension;
	}

	public void setToExtension(int toExtension)
	{
		this.toExtension = toExtension;
	}

	public double getAmount()
	{
		return amount;
	}
	
	public String getFormattedAmount()
	{
		DecimalFormat dcf = new DecimalFormat("#0.00");
		dcf.setDecimalSeparatorAlwaysShown(true);
		return "$"+dcf.format(amount);
	}
	
	public int getDuration()
	{
		//returns duration in minutes rounded up
		double dur = (double)(endTime.getTimeInMillis() - startTime.getTimeInMillis());
		System.out.println(dur);
		dur = dur/60000;
		System.out.println(dur);
		dur = Math.ceil(dur);
		System.out.println(dur);
		return (int)dur;
	}
	
	private Connection getConnection() throws ClassNotFoundException, SQLException
	{
		Class.forName("org.sqlite.JDBC");
		String home = System.getProperty("user.home");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home+ "/db/VoIP");
		
		return conn;
	}
	
}
